package it.unibo.iss.domotica;

import java.util.ArrayList;
import java.util.List;

import it.unibo.iss.domotica.edDomain.Elettrodomestico;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class UsrCmdDialogBuilder {
	
	public static final int LISTEDSUPERFLUI_DIALOG = 0;
	public static final int PROGRESSLOADING_DIALOG = 1;
	public static final int ALERT_DIALOG = 2;
	
	private UsrCmdActivity parent;
	private ArrayList<Elettrodomestico> listEdSuperflui;
	private int selected;
	
	
	public UsrCmdDialogBuilder(UsrCmdActivity parent) {
		this.parent = parent;
	}
	
//	public Dialog buildListEdSuperfluiDialog() {
//		ListEdSuperfluiDialog listEdSuperfluiDialog = new ListEdSuperfluiDialog(parent);
//		listEdSuperfluiDialog.setOnClikListener(new ListEdSuperfluiDialog.IOnClickListener() {
//			@Override
//			public void onPowerOffClick(Elettrodomestico selected) {
//				parent.onPowerOffSuperfluoClick(selected);
//			}
//
//			@Override
//			public void onAbortClick() {
//				parent.onAbortPowerOffSuperfluoClick();
//			}
//		});
//
//		return listEdSuperfluiDialog;
//	}
	
	public AlertDialog buildListEdSuperfluiDialog() {
//		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_list_item_single_choice);
		String[] items = new String[listEdSuperflui.size()];
//		arrayAdapter.clear();
		
		int i = 0;
		for(Elettrodomestico ed : listEdSuperflui) {
//			arrayAdapter.add(ed.getNome() + " - " + ed.getConsumoAttuale());
			items[i++] = ed.getNome() + " - " + ed.getConsumption();
		}
		
		AlertDialog.Builder builder = new AlertDialog.Builder(parent);
		builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				selected = which;
			}
		});
		builder.setCancelable(false);
		builder.setPositiveButton("Power Off", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Elettrodomestico ed = listEdSuperflui.get(selected);
				Toast.makeText(parent, "Selected " + ed, Toast.LENGTH_LONG);
				parent.onPowerOffSuperfluoClick(ed);
			}
		});
		builder.setNegativeButton("Abort", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				parent.onAbortPowerOnClick();
			}
		});
		
		AlertDialog alert = builder.create();
		alert.show();
		
		return alert;
	}
	
	public ArrayList<Elettrodomestico> getListEdSuperflui() {
		return listEdSuperflui;
	}

	public void setListEdSuperflui(ArrayList<Elettrodomestico> listEdSuperflui) {
		this.listEdSuperflui = listEdSuperflui;
	}
	
	public Dialog buildProgressDialogListEd() {
		return ProgressDialog.show(parent, "", "Loading household appliances list...", true, false);
	}
	
	protected String msgToShow;
	
	public Dialog onCreateDialog(int id) {
		switch(id) {
			case LISTEDSUPERFLUI_DIALOG:
				return buildListEdSuperfluiDialog();
	
			case PROGRESSLOADING_DIALOG:
				return buildProgressDialogListEd();
				
			case ALERT_DIALOG:
				AlertDialog.Builder db;
				db = new AlertDialog.Builder(parent);
				db.setMessage(msgToShow);
				db.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				return db.create();
		}

		return null;
	}
	
	protected void showAlarm(String msg) {
		msgToShow = msg;
		parent.showDialog(ALERT_DIALOG);
	}
	
//	protected void showMsgToast(String msg) {
//		Toast.makeText(parent, msg, Toast.LENGTH_SHORT).show();
//	} 
}
